(defun start-programming-mode()
  (interactive)
  (company-mode)
  (def-header)

  ;; Display column numbers only in code.
  (column-number-mode t)
  (flyspell-prog-mode)

  ;; All trailing whitespace needs to be highlighted so it can die.
  (setq show-trailing-whitespace t)

  ;; Highlight matching parenthesis (and other bracket likes)
  (show-paren-mode t))

(defun my-start-scripting-mode (file-extension hash-bang)
  ;; All scripting languages are programming languages
  (start-programming-mode)

  ;; Make the script executable on save
  (add-hook 'after-save-hook
            'executable-make-buffer-file-executable-if-script-p
            nil t))

;;;; ---------------------------- C MODE ------------------------------ ;;;;
(defun my-c-mode-hook ()
  (start-programming-mode)

  (c-set-style "k&r")

  ;; Modify compilations settings
  (unless (file-exists-p "Makefile")
    (set (make-local-variable 'compile-command)
  	 (let ((file (file-name-nondirectory buffer-file-name)))
  	   (concat "gcc -o "
  		   (file-name-sans-extension file)
  		   " " fnnile))))

  ;; Load and start up filladapt
  (c-setup-filladapt)
  (filladapt-mode t))

;;;; --------------------------- C++ MODE ----------------------------- ;;;;
(defun my-c++-mode-hook ()
  (start-programming-mode)

  (c-set-style "k&r")

  ;; Modify compilation settings
  (unless (file-exists-p "Makefile")
    (set (make-local-variable 'compile-command)
  	 (let ((file (file-name-nondirectory buffer-file-name)))
  	   (concat "g++ -o "
  		   (file-name-sans-extension file)
  		   " " file))))

  ;; Load and start up filladapt
  (c-setup-filladapt)
  (filladapt-mode t))

;;;; ------------------------ EMACS-LISP MODE ------------------------- ;;;;
(defun my-lisp-mode-hook ()
  (start-programming-mode))

;;;; --------------------------- SH MODE ------------------------------ ;;;;
;; Sh mode
(defun my-sh-mode-hook ()
  "Setup shell script mode."
  (interactive)
  (my-start-scripting-mode "sh" "#!/bin/bash"))

;;;; --------------------------- TEX MODE ----------------------------- ;;;;
(defun my-tex-mode-hook ()
  "Change the default TeX environment."
  (interactive)
  (start-writing-mode)

  ;; Set up ispell to do the right thing
  (setq ispell-parser 'tex))

;;;; -------------------------- TEXT MODE ----------------------------- ;;;;
(defun my-text-mode-hook ()
  (interactive)
  (start-writing-mode))

;;;; -------------------------- LATEX MODE ---------------------------- ;;;;
(defun my-latex-mode-hook ()
  "Change the default LaTeX environment."
  (interactive)
  (start-writing-mode)

  ;; Compile all Latex files to PDF
  (TeX-PDF-mode t)

  ;; Set up ispell to do the right thing
  (setq ispell-parser 'tex))

;;;; --------------------------- PERL MODE ---------------------------- ;;;;
(defun my-perl-mode-hook ()
  "Setup Perl style."
  (interactive)
  (my-start-scripting-mode "pl" "#!/usr/bin/perl"))

;;;; -------------------------- PYTHON MODE --------------------------- ;;;;
(defun my-python-mode-hook ()
  "Setup Python style."
  (interactive)
  (setq indent-tabs-mode nil)  ; Autoconvert tabs to spaces
  (my-start-scripting-mode "py" "#!/usr/bin/python")
  (local-set-key '[f5] 'python-send-buffer))

;;;; ----------------------- ACTIONSCRIPT MODE ------------------------ ;;;;
(defun my-actionscript-mode-hook ()
  ;; Modify compilation settings
  (start-programming-mode)
  (set (make-local-variable 'compile-command) (concat "mxmlc " (buffer-name))))

;;;; ---------------------------- HTML MODE --------------------------- ;;;;
(defun my-html-mode-hook ()
  (start-programming-mode)
  (setq header-date-format "%B %e, %Y %S"
	make-header-hook '(
			   header-modification-date
			   )))

;;;; -------------------------- ORG MODE ------------------------------ ;;;;
(defun my-org-mode-hook ()
  (setq org-log-done t)
  (auto-fill-mode t)
  (filladapt-mode t))

;;;; -------------------------- MODE HOOKS ----------------------------- ;;;;
(add-hook 'c++-mode-hook 'my-c++-mode-hook)
(add-hook 'c-mode-hook 'my-c-mode-hook)
(add-hook 'scheme-mode-hook 'my-scheme-mode-hook)
(add-hook 'emacs-lisp-mode-hook 'my-lisp-mode-hook)
(add-hook 'lisp-mode-hook 'my-lisp-mode-hook)
(add-hook 'perl-mode-hook 'my-perl-mode-hook)
(add-hook 'sh-mode-hook 'my-sh-mode-hook)
(add-hook 'tex-mode-hook 'my-tex-mode-hook)
(add-hook 'LaTeX-mode-hook 'my-latex-mode-hook)
(add-hook 'python-mode-hook 'my-python-mode-hook)
(add-hook 'actionscript-mode-hook 'my-actionscript-mode-hook)
(add-hook 'html-mode-hook 'my-html-mode-hook)
(add-hook 'xhtml-mode-hook 'my-html-mode-hook)
(add-hook 'OpenGL-minor-mode-hook 'OpenGL-setup-keys)
(add-hook 'org-mode-hook 'my-org-mode-hook)
(add-hook 'text-mode-hook 'my-text-mode-hook)
